<?php
require_once 'config.php';

try {
	$db = new PDO('pgsql:host='.DB_HOST.';dbname='.DB_NAME, DB_USER, DB_PASS);
} catch (PDOException $e) {
	print "Error!: " . $e->getMessage();
	die();
}
$result = '';

$sql = 'SELECT "ID","TITLE","ADDRESS","PRICE","PHONE","TELEGRAM","WHATSAPP","VIBER","ROOM","PHOTOS" FROM "apartment"';
$stmt = $db->query($sql);
$result = '{"apartment":[';


while ($row = $stmt->fetch()) {

	$result .= sprintf('{"id":%d,"title":"%s","address":"%s","price":"%s","phone":"%s","telegram":"%s","whatsapp":"%s","viber":"%s","room":%d,"photos":%d},',$row['ID'],$row['TITLE'],$row['ADDRESS'],$row['PRICE'],$row['PHONE'],$row['TELEGRAM'],$row['WHATSAPP'],$row['VIBER'],$row['ROOM'],$row['PHOTOS']);
}

$result = rtrim($result, ",");
$result .= ']}';

echo $result;

?>